package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

public class NotifyNewSystem implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(NotifyNewSystem.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		
			log.debug("----------------开始执行NotifyNewSystem.Grovvy----------------");
			String charset = "utf-8"; //设置字符集
			int timeout = 100000; //设置请求超时时间
			int enableSSLflag = 0;	
			Map headMap=new HashMap(); //设置头文件
			def selectSQL="";
			def insertSQL="";
			headMap.put("Content-Type","application/json");

			selectSQL = "select t.qt_id,t.offerno,t.quote_no from tabccarinsurance t where t.qt_id not in(select qt_id from dx_tabccarinsurance_s) and ( t.full_policy_no is not null or t.sy_policycode is not null or t.jq_policycode is not null) and substr(t.qt_id, 0, 4) = 'DNTS' and t.paydate>sysdate-1 and rownum<100";
			def resultLists=eliteBean.chaxun_sql(selectSQL, 'dataSource');
			def resultListNums = resultLists.size();
			//log.debug("需要新增的数量:" + resultListNums);
			if(resultListNums>0)
			{
			insertSQL = "insert into dx_tabccarinsurance_s (qt_id,offerno,quote_no,createdate)select t.qt_id,t.offerno,t.quote_no,sysdate from tabccarinsurance t where t.qt_id not in(select qt_id from dx_tabccarinsurance_s where  status='0'or succ='flase') and ( t.full_policy_no is not null or t.sy_policycode is not null or t.jq_policycode is not null) and substr(t.qt_id, 0, 4) = 'DNTS' and t.paydate>sysdate-1 and rownum<500 order by t.paydate desc";
			eliteBean.update(insertSQL, 'dataSource');
			//log.debug("打印新增的订单号、交强险投保单号、商业险投保单号" + insertSQL);
			
			selectSQL = "select qt_id,offerno,quote_no from dx_tabccarinsurance_s where (status='0' or succ='flase') and createdate>sysdate-1";		
			def resultList = eliteBean.chaxun_sql(selectSQL,"dataSource");
			def resultListNum = resultList.size();
			//log.debug("需要打印的调用数量:" + resultListNum);
			
			def selectSQL2="select (WEBSERVICEURL||(select WEBSERVICEURL from sysc_interface where WEBTYPE='CBQRTZXXT')) as WEBSERVICEURL from sysc_interface where WEBTYPE='BJURL'";
			def postURL2=eliteBean.chaxun_sql(selectSQL2, "dataSource");
			def postURL=postURL2[0].get("WEBSERVICEURL");
			//log.debug("打印调用地址:" + postURL);
			
			def QT_ID=""; 
			def OFFERNO="";
			def QUOTE_NO="";	
			def SUCC="";
			def ERRMSG="";

			if(resultListNum>0) 
			{	log.debug("开始调用承保确认通知新系统接口");								
				def postXml="";
				def result="";
				
				for (int i = 0; i < resultListNum; i++){	
					def queryMap1=resultList[i];
					//log.debug("开始执行第"+i+"行");
					QT_ID=queryMap1.get("qt_id");
					if(queryMap1.get("offerno")!=null)
					{
						OFFERNO=queryMap1.get("offerno");
					}
					else{
						OFFERNO="";
					}
					if(queryMap1.get("quote_no")!=null)
					{
						QUOTE_NO=queryMap1.get("quote_no");
					}
					else{
						QUOTE_NO="";
					}
					
					//log.debug("SQL查询的订单号："+ QT_ID);
					//log.debug("SQL查询的交强险投保单号："+ OFFERNO);
					//log.debug("SQL查询的商业险投保单号："+ QUOTE_NO);


					postXml="{\"orderNo\":\""+QT_ID+"\",\n" +"\"proposalNo0801\":\""+OFFERNO+"\",\n" +"\"proposalNo0802\":\""+QUOTE_NO+"\"}";
					//log.debug("打印全报文:" + postXml); 
					log.debug("开始调用打印接口地址:"+postURL+"报文:"+ postXml);
				
					result = Util.doHttpPostUseHead(postURL,postXml,charset,timeout,enableSSLflag,headMap);							 
					//log.debug("打印调用保单打印接口结果:"+result);
									
					JSONObject obj = JSONObject.fromObject(result);
					//log.debug("转JSON对象打印调用保单打印接口结果:"+obj);
									
					SUCC=obj.get("succ");
					log.debug("成功标识，请求失败会返回false:"+SUCC);
	
					if(SUCC==true)
					{
						def updateSQL1="update dx_tabccarinsurance_s set succ='"+SUCC+"',errmsg='"+ERRMSG+"',status='1' where qt_id='"+QT_ID+"'";
						//log.debug("修改调用成功的数据:"+updateSQL1);
						//log.debug("修改成功");
						eliteBean.update(updateSQL1,'dataSource');
					}
					else
					{	
						ErrMsg=obj.get("errMsg");
						//log.debug("返回错误信息描述:"+ErrMsg);	
							
						def updateSQL1 = "update dx_tabccarinsurance_s set succ='"+SUCC+"',errmsg='"+ERRMSG+"',status='1' where qt_id='"+QT_ID+"'";
						eliteBean.update(updateSQL1, 'dataSource');
						//log.debug("修改调用失败的数据" + updateSQL1);
					}
				}
				
				def updateSQL1="delete from dx_tabccarinsurance_s where CREATEDATE<=sysdate-2";
				//log.debug("删除调用成功的数据:"+updateSQL1);
				eliteBean.update(updateSQL1,'dataSource');
				//log.debug("删除成功");
				
				log.debug("-------------->执行完毕！");
			}
		}		
		else{
				log.debug("暂无新增数据无需调用Grovvy");
		}
	}
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}
}